#!/bin/bash

DAEMON_NAME="starkylin_connectivity"
DAEMON_SERVICE_EXEC="starkylin_connectivity_service"
DAEMON_PATH="/usr/bin/$DAEMON_SERVICE_EXEC"
DAEMON_PIDFILE_PATH="/tmp/starkylin_connectivity.pid"
LOG_FILE="/tmp/starkylin_connectivity.log"

start() {
    echo "开始执行 $DAEMON_NAME..."
    # 将输出重定向到日志文件，并后台运行
    nohup $DAEMON_PATH >> $LOG_FILE 2>&1
    echo "后台执行中..."
}

end() {
    if [ -f $DAEMON_PIDFILE_PATH ]; then
        PID=$(cat $DAEMON_PIDFILE_PATH)
        echo "Stopping $DAEMON_NAME..."
        kill $PID
        rm -f $DAEMON_PIDFILE_PATH
        echo "Stopped $DAEMON_NAME."
    else
        echo "$DAEMON_NAME is not running."
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        end
        ;;
    restart)
        end
        start
        ;;
    *)
        echo "Usage: $0  {start|stop|restart}"
        exit 1
        ;;
esac

echo "执行结束"
exit 0